package com.zhongketech.sunflower.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.support.v4.view.ViewPager;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.adapter.ExpressionAdapter;
import com.zhongketech.sunflower.adapter.ExpressionPagerAdapter;
import com.zhongketech.sunflower.adapter.FcsContact;
import com.zhongketech.sunflower.adapter.FcsContactAdapter;
import com.zhongketech.sunflower.adapter.MessageAdapter;
import com.zhongketech.sunflower.adapter.VoicePlayClickListener;
import com.zhongketech.sunflower.control.SpecialNumsManager;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.im.ZKSocket;
import com.zhongketech.sunflower.transport.model.Authorizations;
import com.zhongketech.sunflower.transport.model.MyRequest;
import com.zhongketech.sunflower.transport.model.Parents;
import com.zhongketech.sunflower.transport.model.Request;
import com.zhongketech.sunflower.transport.model.SpecialNums;
import com.zhongketech.sunflower.transport.model.User;
import com.zhongketech.sunflower.utils.CommonUtils;
import com.zhongketech.sunflower.utils.ImageUtil;
import com.zhongketech.sunflower.utils.SmileUtils;




/**
 * 绑定界面
 * 
 */
@SuppressLint("ResourceAsColor")
public class SpecialNumberActivity extends BaseActivity  {


	private static final String TAG = "SpecialNumberActivity";

	private ListView listView ;
	private Context context;

	private List<FcsContact> familyData = new ArrayList<FcsContact>();
	private List<FcsContact> emergencyData = new ArrayList<FcsContact>();
	private Handler handler ;
	private FcsContactAdapter adapter;
	private String type = Constant.SN_FAMILY;
	private int selectColor = 0xFF36c7d1;


	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fcs_specialnumber_activity);
		context = this;
		initView();

		handler = new Handler() {
			public void handleMessage(Message msg) {

				LogUtil.i("ZK","handleMessage type:"+type+" familyData size:"+familyData.size()+" emergencyData size:"+emergencyData.size());
				if(type.equals(Constant.SN_FAMILY))
				{
					for(int i = 0;i < familyData.size();i ++)
					{
						LogUtil.i("ZK","familyData["+i+"] name:"+familyData.get(i).itemText);
					}
					adapter.updateData(familyData,Constant.FCS_ADAPTER_TYPE_UNKNOWN);
				}
				else if(type.equals(Constant.SN_EMERGENCY))
				{
					for(int i = 0;i < emergencyData.size();i ++)
					{
						LogUtil.i("ZK","emergencyData["+i+"] name:"+emergencyData.get(i).itemText);
					}

					adapter.updateData(emergencyData,Constant.FCS_ADAPTER_TYPE_UNKNOWN);
				}
				adapter.notifyDataSetChanged();
			}
		};
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}

	public void emergency(View v)
	{
		((TextView) findViewById(R.id.emergency)).setTextColor(selectColor);
		((TextView) findViewById(R.id.family)).setTextColor(Color.BLACK);
		type = Constant.SN_EMERGENCY;
		final Message msg = new Message();
		handler.sendMessage(msg);
	}

	public void family(View v)
	{
		((TextView) findViewById(R.id.emergency)).setTextColor(Color.BLACK);
		((TextView) findViewById(R.id.family)).setTextColor(selectColor);
		type = Constant.SN_FAMILY;
		final Message msg = new Message();
		handler.sendMessage(msg);
	}

	private void initData()
	{

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();
				List<SpecialNums> list = new Action().specialnums();
				if(null != list)
				{
					familyData.clear();
					emergencyData.clear();
					for(int i = 0;i < list.size();i ++)
					{
						if(Constant.SN_WHITLE.equals(list.get(i).getType()))
						{
							LogUtil.i("ZK","SpecialNums["+i+"]:"+list.get(i).getGroup()+" "+list.get(i).getName()+"  "+list.get(i).getPhone());
							FcsContact ad = new FcsContact();
							ad.imageViewId = -1;
							ad.itemText = list.get(i).getName();
							ad.statusText = null;
							ad.textview2 =  list.get(i).getPhone();
							if(Constant.SN_FAMILY.equals(list.get(i).getGroup()))
							{
								LogUtil.i("ZK","add familyData");
								familyData.add(ad);
							}
							else if(Constant.SN_EMERGENCY.equals(list.get(i).getGroup()))
							{
								LogUtil.i("ZK","add emergencyData");
								emergencyData.add(ad);
							}
						}
					}

					final Message msg = new Message();
					handler.sendMessage(msg);
					SpecialNumsManager.save(list);
				}
				else
				{
					MyToast.getInstance().show("亲情号码为空");
				}

			}
		}).start();

	}

	private void initView()
	{
		((TextView) findViewById(R.id.title)).setText("特殊号码");
		((TextView) findViewById(R.id.family)).setTextColor(selectColor);
		listView = (ListView) findViewById(R.id.listView);
		adapter = new FcsContactAdapter(familyData,Constant.FCS_ADAPTER_TYPE_BIND);
		listView.setAdapter(adapter);
	}
}
